package com.easidea.tim.flex.remote.saleorder
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.SysLocator;
	import com.easidea.tim.flex.bean.saleorder.SaleOrder;
	import com.easidea.tim.flex.bean.saleorder.SaleOrderLocator;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	import com.easidea.tim.flex.util.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.NumberFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	

	public class SaleOrderDao extends BaseDao
	{
		
		private var _saleOrderService:RemoteObject = new RemoteObject();
		[Bindable]private var saleOrderLocator:SaleOrderLocator = SaleOrderLocator.getInstance();
		private static var _instance:SaleOrderDao;
		private var _orderId:int;
		private var _ordertype:String;
		private var _status:String;
		
		public static function getInstance():SaleOrderDao{
			if( !_instance ){
				_instance = new SaleOrderDao();
				
			}
			return _instance;
		}
		public function SaleOrderDao() {
				_saleOrderService.channelSet = this.buildChannelSet();
				_saleOrderService.destination="saleOrderService";
				_saleOrderService.concurrency = "last";
				_saleOrderService.makeObjectsBindable = true;	
				_saleOrderService.showBusyCursor = true;
		}		
		
				/**
		 * 调用spring方法:分页查询采购合同列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getSaleOrderList(creator:String,saleOrderNum:String, customerName:String,startTime:Date,endTime:Date,ordertype:String,page:Page,status:String,order:String):void {			
			_saleOrderService.addEventListener(ResultEvent.RESULT, showSaleOrderList);
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.getSaleOrderListByCondition(creator,saleOrderNum,customerName,startTime,endTime,ordertype,page,status,order);
		}
		
		public function getSaleOrderListByCreator(creator:String,saleOrderNum:String, customerName:String,startTime:Date,endTime:Date,ordertype:String,page:Page,status:String,creatorName:String,order:String):void {			
			_saleOrderService.addEventListener(ResultEvent.RESULT, showSaleOrderList);
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.getSaleOrderListByCreator(creator,saleOrderNum,customerName,startTime,endTime,ordertype,page,status,creatorName,order);
		}
		
		public function getSaleOrderById(id:String):void{
			_saleOrderService.addEventListener(ResultEvent.RESULT, showSaleOrder);
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.GetSaleOrderByid(id);
		}
		
		private function showSaleOrder(e:ResultEvent):void {	 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, showSaleOrder);
			 if(e.result != null) {		
				var saleOrder:SaleOrder = e.result as SaleOrder;				 				 
				 Env.dispatchEvent(new CustomerEvent("getSaleOrderDetail",saleOrder));				  									 
			 } 
		}
		/**
		 * 分页查询销售订单列表回调函数
		 * @param e
		 * 
		 */			
		private function showSaleOrderList(e:ResultEvent):void {	
					 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, showSaleOrderList);
			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				 Env.dispatchEvent(new CustomerEvent("getSaleOrderListSuccess",page));				  									 
			 } 
		}
			/**
		 * 调用spring方法:新增或修改销售订单需要的初始化数据
		 * @param name
		 * @param page
		 * 
		 */	
		public function getInitDataList(id:int):void {
			_orderId = id;
			_saleOrderService.addEventListener(ResultEvent.RESULT, showInitDataList);
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.getInitDataList();
			
		}

		
		private function showInitDataList(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, showInitDataList);			
			 var result:ArrayCollection = e.result as ArrayCollection; 	 
			 if(result != null ) {		
					Env.dispatchEvent(new CustomerEvent("getSaleOrderSuccess",result));
				}
		}

		
		/**
		 *调用spring方法:保存销售订单和明细列表 
		 * @param customer
		 * 
		 */		
		public function saveSaleOrder(order:SaleOrder,items:ArrayCollection,ordertype:String,status:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, saveSaleOrderSuccess);
				_ordertype = ordertype;
				_status = status;
				_saleOrderService.saveSaleOrder(order,items);
		}
		
		/**
		 *调用spring方法:保存销售订单和明细列表 
		 * @param customer
		 * 
		 */		
		public function updateSaleOrder(order:SaleOrder,items:ArrayCollection,ordertype:String,status:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, saveSaleOrderSuccess);
				_ordertype = ordertype;
				_status = status;
				_saleOrderService.updateSaleOrder(order,items);
		}
	   /**
	    *保存销售订单和明细列表成功回调函数 
	    * @param e
	    * 
	    */		
	   private function saveSaleOrderSuccess(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, saveSaleOrderSuccess);
			 if(e.result != null) {
				AppAlert.show("保存成功！","提示");
				getSaleOrderList(String(SysLocator.getInstance().loggedUserId),"","",null,null,_ordertype,new Page(),_status,"modify");
				Env.dispatchEvent(new CustomerEvent("saveSaleOrderSuccess"));
			 } 
		}
		
		public function completeOrder(order:SaleOrder,items:ArrayCollection,ordertype:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, completeSaleOrderSuccess);
				_ordertype = ordertype;
				_saleOrderService.CompleteUpdate(order,items);
		}
	   /**
	    *保存销售订单和明细列表成功回调函数 
	    * @param e
	    * 
	    */		
	   private function completeSaleOrderSuccess(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, completeSaleOrderSuccess);
			 if(e.result != null) {
				AppAlert.show("填充成功！","提示");
				getSaleOrderList(String(SysLocator.getInstance().loggedUserId),"","",null,null,_ordertype,new Page(),"1","modify");
				Env.dispatchEvent(new CustomerEvent("completeSaleOrderSuccess"));
			 } 
		}
		/**
		 *调用spring方法:删除销售订单
		 * @param customer
		 * 
		 */		
		public function deleteSaleOrder(id:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, deleteSaleOrderSuccess);
				_saleOrderService.deleteSaleOrder(id);
		}
	   /**
	    * 删除销售订单成功回调函数 
	    * @param e
	    * 
	    */		
	   private function deleteSaleOrderSuccess(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, deleteSaleOrderSuccess);
			 if(e.result != null&& e.result == "success") {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new  CustomerEvent("reGetSaleOrderList"));
			 } 
		}
		
		public function JudgeisExistProduct(productName:String,color:String,price:Number):void{
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.addEventListener(ResultEvent.RESULT, isExistProductSuccess);
			_saleOrderService.isExistProduct(productName,color,price);
		}
		
		public function JudgeisExistProductNormal(productName:String,color:String):void{
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.addEventListener(ResultEvent.RESULT, isExistProductSuccess);
			_saleOrderService.isExistProduct(productName,color);
		}
		
		private function isExistProductSuccess(e:ResultEvent):void{
			_saleOrderService.removeEventListener(ResultEvent.RESULT, isExistProductSuccess);
			Env.dispatchEvent(new  CustomerEvent("isExistProduct",e.result));
			
		}
		
		
		/**
		 * 
		public function JudgeisCompleteProduct(productName:String,color:String,flowNo:String,meter:String):void{
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.addEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			_saleOrderService.isCompleteProduct(productName,color,flowNo,meter);
		}
		
		private function isCompleteProductSuccess(e:ResultEvent):void{
			_saleOrderService.removeEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			Env.dispatchEvent(new  CustomerEvent("isCompleteProduct",e.result));
			
		}
		
		/**
		 *调用spring方法：更新订单状态
		 * @param id
		 * @param closeMan
		 * 
		 */		
		public  function updateSaleOrderStatus(id:int, status:int,responser:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, updateSaleOrderStatusSuccess);
				_saleOrderService.updateSaleOrderStatus(id,status,responser);				
		}
		
		public  function updateSaleOrderSend(id:int,responser:String):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT, updateSaleOrderStatusSuccess);
				_saleOrderService.updateSaleOrderSend(id,responser);				
		}
	  /**
	    * 合同关闭回调函数 
	    * @param e
	    * 
	    */		
	   private function updateSaleOrderStatusSuccess(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, updateSaleOrderStatusSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("销售订单状态已更改！","提示");
			    Env.dispatchEvent(new  CustomerEvent("reGetSaleOrderList"));
			 } 
		}
		/**
		 * 
		 * 调用spring方法：获取产品信息
		 */		
		public  function getAllProducts():void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_saleOrderService.addEventListener(ResultEvent.RESULT,getAllProductsSuccess);
				_saleOrderService.getAllProducts();				
		}
	   /**
	    * 取产品信息成功回调函数 
	    * @param e
	    * 
	    */		
	   private function getAllProductsSuccess(e:ResultEvent):void {			 
			_saleOrderService.removeEventListener(ResultEvent.RESULT, getAllProductsSuccess);
			 if(e.result != null) {
			   	Env.dispatchEvent(new  CustomerEvent("getAllProductsSuccess",e.result));					    
			 } 
		}
		/**
		 * 调用spring方法：销售统计
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */	
		public  function statSaleOrder(customerName:String,staffName:String,startDate:Date, endDate:Date,statType:String):void {
				saleOrderLocator.startDate = startDate;
				saleOrderLocator.endDate = endDate;	
				saleOrderLocator.staffname=staffName;
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,statSaleOrderSuccess);
				_saleOrderService.statSaleOrder(customerName,staffName,startDate,endDate,statType);				
		  }
		/**
		 * 销售统计回调函数
		 * @param e
		 * 
		*/  
		private function statSaleOrderSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, statSaleOrderSuccess);
			if(e.result != null) {
				saleOrderLocator.statResult.removeAll();
				var pitotal:Number=0;
				var metertotal:Number = 0;
				var moneyTotal:Number = 0;
				var nf:NumberFormatter = new NumberFormatter();
				var nfone:NumberFormatter = new NumberFormatter();
				nf.precision =2;
				nfone.precision =1;
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.color = result[1];
					obj.piNum = result[2];
					obj.meterNum = result[3];
					obj.moneyNum = result[4];
					saleOrderLocator.statResult.addItem(obj);
					pitotal = pitotal + Number(result[2]);
					metertotal = metertotal + Number(result[3]);
					moneyTotal = moneyTotal + Number(result[4]);
				}
				saleOrderLocator.piTotal = StringUtils.formatNumString(pitotal.toFixed(1));
				saleOrderLocator.meterTotal = StringUtils.formatNumString(metertotal.toFixed(1));
				saleOrderLocator.moneyTotal = StringUtils.formatNumString(moneyTotal.toFixed(3));
			}
		}
		
				/**
		 * 调用spring方法：查看客户统计明细
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */	
		public  function customerDetail(startDate:Date, endDate:Date,id:String):void {		
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,customerDetailSuccess);
				_saleOrderService.getcustomerDetail(startDate,endDate,id);				
		  }
		/**
		 * 查看客户统计明细回调函数
		 * @param e
		 * 
		*/  
		private function customerDetailSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, customerDetailSuccess);
			if(e.result != null) {
				saleOrderLocator.statDetail.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.color = result[1];
					obj.piNum = result[2];
					obj.meterNum = result[3];
					obj.moneyNum = result[4];
					saleOrderLocator.statDetail.addItem(obj);
				}
			}
		}
		
		/**
		 * 查看客户汇总明细
		 * @param e
		 * 
		*/  
		public  function customerDetailSimple(startDate:Date, endDate:Date,id:String):void {		
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,customerDetailSimpleSuccess);
				_saleOrderService.getcustomerDetailSimple(startDate,endDate,id,saleOrderLocator.staffname);				
		  }
		/**
		 * 查看客户汇总明细回调函数
		 * @param e
		 * 
		*/  
		private function customerDetailSimpleSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, customerDetailSimpleSuccess);
			if(e.result != null) {
				saleOrderLocator.statDetail.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.piNum = result[1];
					obj.meterNum = result[2];
					obj.moneyNum = result[3];
					saleOrderLocator.statDetail.addItem(obj);
				}
			}
		}
		
		/**
		 * 调用spring方法：业务员业绩统计明细
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */	
		public  function creatorDetail(startDate:Date, endDate:Date,id:String):void {		
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,creatorDetailSuccess);
				_saleOrderService.getcreatorDetail(startDate,endDate,id);				
		  }
		/**
		 * 销业务员业绩统计明细回调函数
		 * @param e
		 * 
		*/  
		private function creatorDetailSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, creatorDetailSuccess);
			if(e.result != null) {
				saleOrderLocator.statDetail.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.piNum = result[1];
					obj.meterNum = result[2];
					obj.moneyNum = result[3];
					saleOrderLocator.statDetail.addItem(obj);
				}
			}
		}
		/**
		 * 调用spring方法：业绩月报
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */			
		public  function statSaleOrderCreatorByMonth(startDate:Date, endDate:Date):void {
				saleOrderLocator.startDate = startDate;
				saleOrderLocator.endDate = endDate;
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,statSaleOrderCreatorByMontSuccess);
				_saleOrderService.statSaleOrderCreatorByMonth(startDate,endDate);				
		  }
		  
		public function updateMemo(saleorder:SaleOrder,memo:String):void{
			_saleOrderService.UpdateMemo(saleorder,memo);		
		}
		 
		/**
		 * 业绩月报回调函数
		 * @param e
		 * 
		*/	  
		private function statSaleOrderCreatorByMontSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, statSaleOrderCreatorByMontSuccess);
			if(e.result != null) {
				var pitotal:Number=0;
				var metertotal:Number = 0;
				var moneyTotal:Number = 0;
				var nf:NumberFormatter = new NumberFormatter();
				var nfone:NumberFormatter = new NumberFormatter();
				nf.precision =2;
				nfone.precision =1;
				saleOrderLocator.statResult.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.type= result[1];
					obj.piNum = result[2];
					obj.meterNum = result[3];
					obj.moneyNum = result[4];
					saleOrderLocator.statResult.addItem(obj);
					pitotal = pitotal + Number(result[2]);
					metertotal = metertotal + Number(result[3]);
					moneyTotal = moneyTotal + Number(result[4]);
				}
				saleOrderLocator.piTotal = StringUtils.formatNumString(pitotal.toFixed(1));
				saleOrderLocator.meterTotal = StringUtils.formatNumString(metertotal.toFixed(1));
				saleOrderLocator.moneyTotal = StringUtils.formatNumString(moneyTotal.toFixed(3));
			}
		} 	
		
				/**
		 * 调用spring方法：销售月报
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */			
		public  function statSaleOrderProductByMonth(year:int, month:int):void {
				_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_saleOrderService.addEventListener(ResultEvent.RESULT,statSaleOrderProductByMontSuccess);
				_saleOrderService.statSaleOrderProductMonth(year,month);				
		  }
		 
		/**
		 * 销售月报回调函数
		 * @param e
		 * 
		*/	  
		private function statSaleOrderProductByMontSuccess(e:ResultEvent):void {
			_saleOrderService.removeEventListener(ResultEvent.RESULT, statSaleOrderProductByMontSuccess);
			if(e.result != null) {
				var pitotal:Number=0;
				var metertotal:Number = 0;
				var moneyTotal:Number = 0;

				saleOrderLocator.statResult.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.color= result[1];
					obj.piNum= result[2]; 
					obj.meterNum = result[3];
					obj.moneyNum = result[4];
					saleOrderLocator.statResult.addItem(obj);
					pitotal = pitotal + Number(result[2]);
					metertotal = metertotal + Number(result[3]);
					moneyTotal = moneyTotal + Number(result[4]);
				}
				saleOrderLocator.piTotal = StringUtils.formatNumString(pitotal.toFixed(1));
				saleOrderLocator.meterTotal = StringUtils.formatNumString(metertotal.toFixed(1));
				saleOrderLocator.moneyTotal = StringUtils.formatNumString(moneyTotal.toFixed(3));
			}
		}
		
		
		public function queryCustomerProductPrice(customerName:String, productName:String,colorCode:String):void{
			_saleOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_saleOrderService.addEventListener(ResultEvent.RESULT, queryCustomerProductPriceProcess);
			_saleOrderService.getCustomerProductPrice(customerName, productName, colorCode);
		}
		
		private function queryCustomerProductPriceProcess(e:ResultEvent):void{
			if(e.result != null){
				var rtnArray:ArrayCollection = new ArrayCollection();
				for each(var result:Array in e.result) {
					var data:Object = {customerName:result[0],productName:result[1],colorCode:result[2],isSample:result[3] == 1 ? true:false, money:result[4]};
					rtnArray.addItem(data);
				}
				Env.dispatchEvent(new CustomerEvent("queryCustomerProductPriceSuccess",rtnArray));
			}
		}
	}
}